import {
    AddGoodsCart,
    GetShopCart,
    GetAllChecke,
    SetGoodChecke,
    DeleteOneGood,
    DeleteAll
} from "@/api"


const actions = {
    //添加购物车
    async getAddToCart(miniStore, {
        skuID,
        skuNum
    }) {
        const re = await AddGoodsCart(skuID, skuNum);
        if (re.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject(new Error("加入购物车失败"))
        }
    },

    //获取购物车列表数据
    async getShopCart(miniStore) {
        const re = await GetShopCart();
        if (re.code === 200) {
            miniStore.commit("SET_SHOPCARTLIST_STATE", re.data)
        }
    },
    //修改全选属性
    async setAllChecke(miniStore, {
        isChecked,
        skuIdList
    }) {
        const re = await GetAllChecke(isChecked, skuIdList)
        if (re.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject(new Error("修改全选未成功"))
        }
    },

    //修改单个商品选中状态
    async setOneChecke(miniStore, {
        skuID,
        isChecked
    }) {
        const re = await SetGoodChecke(skuID, isChecked)
        if (re.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject(new Error("单个商品修改状态未成功"))
        }
    },

    async DeleteOne(miniStore, skuId) {
        const re = await DeleteOneGood(skuId)
        if (re.code === 200) {
            return Promise.resolve()
        } else {
            return Promise.reject(new Error("删除单个商品失败"))
        }
    },

    async  DeleteEveryOne(miniStore,skuIdList){
        const re = await DeleteAll(skuIdList)
        if(re.code === 200){
            return Promise.resolve()
        }else{
            return Promise.reject(new Error("删除所有选中商品失败"))
        }
    } 
};
const mutations = {
    SET_SHOPCARTLIST_STATE(state, value) {
        state.ShopCartList = value
    }
};
const state = {
    ShopCartList: []
};
const getters = {
    ShopCartList(state) {
        return state.ShopCartList[0] || {}
    }
}


export default {
    actions,
    mutations,
    state,
    getters
}